package com.leiduoduo.oms.controller;

import com.leiduoduo.oms.annotation.PassToken;
import com.leiduoduo.oms.dao.DishDao;
import com.leiduoduo.oms.dao.DishTypeDao;
import com.leiduoduo.oms.pojo.DishType;
import com.leiduoduo.oms.service.DishTypeService;
import com.leiduoduo.oms.util.PageUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.lang.reflect.InvocationTargetException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@CrossOrigin
public class DishTypeController {
    @Autowired
    DishTypeService dishTypeService;
    @Autowired
    DishTypeDao dishTypeDao;

    @PostMapping("/addDishType")
    @PassToken
    public boolean addDishType(DishType dishType){
        return dishTypeService.addDishType(dishType);
    }

    /**
     * 删除分类
     * @param dishType
     * @return
     */
    @PostMapping("/delDishType")
    @PassToken
    public Map<String,Object> delDishType(DishType dishType){
        Map<String,Object> result = new HashMap<>();
        if (dishTypeService.delDishType(dishType)){
            result.put("msg","删除成功");
            result.put("errorcode",0);
        } else {
            result.put("msg","删除失败");
            result.put("errorcode",0);
        }
        return result;
    }

    /**
     * 编辑分类
     * @param dishType
     * @return
     */
    @PostMapping("/upDishType")
    @PassToken
    public Map<String,Object> upDishType(DishType dishType){
        Map<String,Object> result = new HashMap<>();
        if (dishTypeService.upDishType(dishType)){
            result.put("msg","修改成功");
            result.put("errorcode",0);
        } else {
            result.put("msg","修改失败");
            result.put("errorcode",0);
        }
        return result;
    }
    @GetMapping("/getDishTypeById/{id}")
    @PassToken
    public DishType getDishTypeById(@PathVariable("id") int id){
        return dishTypeService.getDishTypeById(id);
    }
    @GetMapping("/addDishTypeList")
    @PassToken
    public List<DishType> addDishTypeList(){
        return dishTypeService.getDishTypeList();
    }

    /**
     * 获取分类列表（分页）
     * @param restaurant_id 餐馆id
     * @param current 当前页数
     * @param pageSize 总页数
     * @return
     * @throws ClassNotFoundException
     * @throws NoSuchMethodException
     * @throws InstantiationException
     * @throws IllegalAccessException
     * @throws InvocationTargetException
     */
    @GetMapping("/getDishTypeByPage")
    @PassToken
    public Map<String, Object> getDishTypeByPage(int restaurant_id, int current, int pageSize) throws ClassNotFoundException, NoSuchMethodException, InstantiationException, IllegalAccessException, InvocationTargetException {
        Map<String,Object> result = new HashMap<>();
        Map<String,Object> map = new HashMap<>();
        map.put("restaurant_id",restaurant_id);
        String serviceName = "com.leiduoduo.oms.service.DishTypeServiceImpl";
        String methodName = "getDishTypeListByPage";
        String totalMothedName = "getDishTypeListCount";
        Map<String,Object> data = new PageUtil().page(current,pageSize,serviceName,methodName,totalMothedName,dishTypeDao,map );
        result.put("data", data);
        result.put("msg","success");
        result.put("errorcode",0);
        return result;
    }
}
